﻿CREATE PROCEDURE [Mapping].[AddCategory]
	@catagoryName nvarchar(20),
	@description nvarchar(200) = NULL,
  @parent hierarchyid = NULL
AS
  SELECT @parent = ISNULL(@parent, hierarchyid::GetRoot())
	INSERT INTO Mapping.Category(CategoryName, Description, HierarchyId)
    VALUES(@catagoryName, @description, @parent.GetDescendant((SELECT MAX([HierarchyId]) FROM Mapping.Category WHERE [HierarchyId].GetAncestor(1) = @parent), NULL))
RETURN @@IDENTITY
